﻿// -----------------------------------------------------------------------
// <copyright file="DmKhoDataAccess.cs" company="">
// TODO: Copyright by dangppsoft@gmail.com.
// </copyright>
// -----------------------------------------------------------------------
namespace ManagerClinic.Core.DataAccess
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using ManagerClinic.Core.Common;
    using System.Data.Common;
    using System.Data;
    using Microsoft.Practices.EnterpriseLibrary.Data;
    using Microsoft.Practices.EnterpriseLibrary.Common;
    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
    using ManagerClinic.Ultilities.Ultility;

    public class DmKhoDataAccess
    {
        public bool InsertDmKho(DmKhoEntity obj)
        {
            try
            {
                string sql = "spDmKho_Insert";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaKho", DbType.String, obj.MaKho);
                db.AddInParameter(cmd, "TenKho", DbType.String, obj.TenKho);
                db.AddInParameter(cmd, "DiaChi", DbType.String, obj.DiaChi);
                db.AddInParameter(cmd, "DienThoai", DbType.String, obj.DienThoai);
                db.AddInParameter(cmd, "MoTa", DbType.String, obj.MoTa);
                db.AddInParameter(cmd, "MaKhoa", DbType.String, obj.MaKhoa);
                db.AddInParameter(cmd, "TrangThaiSuDung", DbType.Boolean, obj.TrangThaiSuDung);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("InsertDmKho", ex.Message); throw ex; }
        }

        public bool EditDmKho(DmKhoEntity obj)
        {
            try
            {
                string sql = "spDmKho_Update";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaKho", DbType.String, obj.MaKho);
                db.AddInParameter(cmd, "TenKho", DbType.String, obj.TenKho);
                db.AddInParameter(cmd, "DiaChi", DbType.String, obj.DiaChi);
                db.AddInParameter(cmd, "DienThoai", DbType.String, obj.DienThoai);
                db.AddInParameter(cmd, "MoTa", DbType.String, obj.MoTa);
                db.AddInParameter(cmd, "MaKhoa", DbType.String, obj.MaKhoa);
                db.AddInParameter(cmd, "TrangThaiSuDung", DbType.Boolean, obj.TrangThaiSuDung);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("UpdateDmKho", ex.Message); throw ex; }
        }

        public bool DeleteDmKho(string maKho)
        {
            try
            {
                string sql = "spDmKho_DeleteByID";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaKho", DbType.String, maKho);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("DeleteDmKho", ex.Message); throw ex; }
        }

        public DmKhoEntity GetDmKho(string maKho)
        {
            try
            {
                DmKhoEntity obj = null;
                string sql = "spDmKho_SelectByID";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaKho", DbType.String, maKho);
                using (IDataReader reader = db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        obj = new DmKhoEntity();
                        obj.MaKho = reader["MaKho"].ToString();
                        obj.TenKho = reader["TenKho"].ToString();
                        obj.DiaChi = reader["DiaChi"].ToString();
                        obj.DienThoai = reader["DienThoai"].ToString();
                        obj.MoTa = reader["MoTa"].ToString();
                        obj.MaKhoa = reader["MaKhoa"].ToString();
                        obj.TrangThaiSuDung = String.IsNullOrEmpty(reader["TrangThaiSuDung"].ToString()) == true ? false : (bool)reader["TrangThaiSuDung"];
                    }
                }
                return obj;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("GetDmKho", ex.Message); throw ex; }
        }

        public List<DmKhoEntity> GetDmKhoList()
        {
            try
            {
                List<DmKhoEntity> list = new List<DmKhoEntity>();
                DmKhoEntity obj = null;
                string sql = "spDmKho_SelectAll";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                using (IDataReader reader = db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        obj = new DmKhoEntity();
                        obj.MaKho = reader["MaKho"].ToString();
                        obj.TenKho = reader["TenKho"].ToString();
                        obj.DiaChi = reader["DiaChi"].ToString();
                        obj.DienThoai = reader["DienThoai"].ToString();
                        obj.MoTa = reader["MoTa"].ToString();
                        obj.MaKhoa = reader["MaKhoa"].ToString();
                        obj.TrangThaiSuDung = String.IsNullOrEmpty(reader["TrangThaiSuDung"].ToString()) == true ? false : (bool)reader["TrangThaiSuDung"];
                        list.Add(obj);
                    }
                }
                return list;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("GetDmKhoList", ex.Message); throw ex; }
        }

    }
}